package com.sysware.mymodule.dto;


import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;
import java.util.*;

/**
 * @author : P2M.liup
 * @version : 2.9.6
 * @copyright : Sysware Technology Co., Ltd
 * @date : 2018/11/26 10:28
 */
@ApiModel(description= "服务端响应数据")
public class Response implements Serializable {
    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "错误信息")
    private List<Meta> errors;

    @ApiModelProperty(value = "响应数据条目")
    private Object data;

    @ApiModelProperty(value = "响应数据条目数")
    private Long count;

    /**
     * constructor
     */
    public Response() {
    }

    /**
     * constructor
     *
     * @param errors
     * @param data
     */
    public Response(List<Meta> errors, Object data) {
        this.errors = errors;
        this.data = data;
    }


    /**
     * constructor
     *
     * @param data
     */
    public Response(Object data) {
        this.data = data;
    }

    public Response successCount(Object data, Long count) {
        this.data = data;
        this.count = count;
        return this;
    }

    /**
     * 成功返回单个简单的信息
     *
     * @param key
     * @param value
     * @return
     */
    public Response success(String key, Object value) {
        Map<String, Object> datas = new HashMap<String, Object>();
        datas.put(key, value);
        this.data = datas;
        return this;
    }

    /**
     * success方法
     *
     * @param data
     * @return
     */
    public Response success(Object data) {
        this.data = data;
        return this;
    }

    /**
     * 返回单个失败信息
     *
     * @param message
     * @param code
     * @return
     */
    public Response failure(String message, String code) {
        Response.Meta meta = new Response.Meta(message, code);
        this.errors = new ArrayList<>();
        this.errors.add(meta);
        return this;
    }

    /**
     * 返回多个失败信息
     *
     * @param errorMessage
     * @return
     */
    public Response failure(Map<String, String> errorMessage) {
        this.errors = new ArrayList<>(errorMessage.size());
        Set<Map.Entry<String, String>> entries = errorMessage.entrySet();
        for (Map.Entry<String, String> entry : entries) {
            Response.Meta meta = new Response.Meta(entry.getKey(), entry.getValue());
            this.errors.add(meta);
        }
        return this;
    }

    /**
     * gettter
     *
     * @return
     */
    public List<Meta> getErrors() {
        return errors;
    }

    /**
     * setter
     *
     * @param errors
     */
    public void setErrors(List<Meta> errors) {
        this.errors = errors;
    }

    /**
     * getter
     *
     * @return
     */
    public Object getData() {
        return data;
    }

    /**
     * setter
     *
     * @param data
     */
    public void setData(Object data) {
        this.data = data;
    }

    /**
     * getter
     *
     * @return
     */
    public Long getCount() {
        return count;
    }

    /**
     * setter
     *
     * @param count
     */
    public void setCount(Long count) {
        this.count = count;
    }

    /**
     * sub-class
     */
    @ApiModel(description= "响应错误信息")
    public class Meta {
        @ApiModelProperty(value = "详细信息")
        private String message;

        @ApiModelProperty(value = "错误码")
        private String code;


        public Meta(String message) {
            this.message = message;
        }

        public Meta(String message, String code) {
            this.message = message;
            this.code = code;
        }

        public String getMessage() {
            return message;
        }

        public void setMessage(String message) {
            this.message = message;
        }

        public String getCode() {
            return code;
        }

        public void setCode(String code) {
            this.code = code;
        }
    }

}

